package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.View;

import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserAddCardActivityBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.mvp.contract.IUserAddCardContact;
import sz.szsmk.citizencard.mvp.presenter.UserAddCardPresenter;
import sz.szsmk.citizencard.ui.adapter.UserAddCardAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 10:17
 * description:我的切换卡片
 */
@Route("useraddcard")
public class UserAddCardActivity extends IBaseActivity<UserAddCardPresenter, UserAddCardActivityBinding>
        implements IUserAddCardContact.IUserAddCardView, View.OnClickListener {


    private int myPosition;
    private UserAddCardAdapter addCardAdapter;
    private List<QueryUserCardResponse.DataBean> mData = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private String cardNo;

    private String tempCardNo;

    @Override
    protected int attachLayoutRes() {
        return R.layout.user_add_card_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        swipeRefreshLayout = dataBinding.swipeRefreshLayout;
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.queryUserCard();
//                mPresenter.getUserBindCard();
            }
        });

        cardNo = getIntent().getStringExtra("cardNo");

        dataBinding.titleLayout.settingTitle.setText("添加卡片");
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.userAddTv.setOnClickListener(this);
        RecyclerView userAddCardRv = dataBinding.userAddCardRv;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        userAddCardRv.setLayoutManager(linearLayoutManager);
        addCardAdapter = new UserAddCardAdapter(this, mData);
        addCardAdapter.setCardNo(cardNo);

        addCardAdapter.delSetOnClick(new UserAddCardAdapter.DelArrressInterface() {
            @Override
            public void onClick(View view, int position) {
                QueryUserCardResponse.DataBean dataBean = mData.get(position);
                tempCardNo = dataBean.getBindCardNo();
                L.e(Constant.TAG, "bindCardNo====" + tempCardNo);
                mPresenter.addUserCardFav(dataBean);
            }
        });
        userAddCardRv.setAdapter(addCardAdapter);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        swipeRefreshLayout.setRefreshing(true);
        mPresenter.queryUserCard();
    }

    @Override
    protected void updateViews() {
    }

    @Override
    public void updataError() {

    }

    @Override
    public void queryUserCardSucc(final List<QueryUserCardResponse.DataBean> data) {
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                mData.clear();
                if (data != null) {
//                    dataBinding.noNetwork.setVisibility(View.GONE);
                    for (int i = 0; i < data.size(); i++) {
                        QueryUserCardResponse.DataBean dataBean = data.get(i);
                        String stateName = dataBean.getStateName();
                        if (stateName.equals("已开通") || stateName.equals("已过期")) {
                            mData.add(dataBean);
                        }
                    }
                    L.e(Constant.TAG, "queryUserCardSucc成功");
                    addCardAdapter.notifyDataSetChanged();
                } else {
//                    dataBinding.noNetwork.setVisibility(View.VISIBLE);
                }
            }
        }, 200);
    }

    @Override
    public void queryUserCardFail(QueryUserCardResponse response) {
//        dataBinding.noNetwork.setVisibility(View.VISIBLE);

        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void addUserCardFavSucc(IbaseBean response) {
        cardNo = tempCardNo;
        addCardAdapter.setCardNo(cardNo);
        addCardAdapter.notifyDataSetChanged();

        final SuccessDialog dialog = new SuccessDialog(this, response.getMessage());
        dialog.show();

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dialog.cancel();
            }
        }.start();
    }

    @Override
    public void addUserCardFavFail(String message) {
        final SuccessDialog dialog = new SuccessDialog(this, message);
        dialog.setOnOkClickListener(new SuccessDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.cancel();
            }
        });
        dialog.show();
    }

    @Override
    public void getBindCardSucc(String msg) {

    }

    @Override
    public void getBindCardFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.user_add_tv) {
            Bundle bundle = new Bundle();
            bundle.putString("key", "user");
            JumpUtils.jumptoNatvie(this, "addnewcard", 0, bundle);
        }
    }

}
